package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;


@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private  ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;

    @Override
    public PageResult getItemList(PageResult pageResult) {
        System.out.println(pageResult);
        /**
         * 业务说明:  完成商品查询操作
         * URL: /item/getItemList?query=&pageNum=1&pageSize=10
         * 参数: pageResult
         * 类型: get/delete    post/put
         * 返回值: SysResult对象
         */
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", pageResult.getQuery());

        //2.完成分页操作
        //2.1 准备一个分页对象,MP基于初始化条件,获取分页其它数据
        IPage<Item> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page=itemMapper.selectPage(page, queryWrapper);
        //2.2 获取总数,获取记录数
        long total=page.getTotal();
        List<Item> list =page.getRecords();

        return pageResult.setTotal(total).setRows(list);
    }

    @Override
      @Transactional//事务控制
    public void updateItemStatus(Item item) {
        //QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
       // itemMapper.update(item,queryWrapper);
        item.setUpdated(new Date());
        itemMapper.updateById(item);

    }

    //删除
    @Override
    public void deleteItemById(Integer id) {
        itemMapper.deleteById(id);
    }

    //商品入库
    @Override
    public void saveItem(ItemVO itemVO) {
        Item item = itemVO.getItem();
        Date date = new Date();
        item.setStatus(true).setCreated(date).setUpdated(date);
        itemMapper.insert(item);

        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId()).setCreated(date).setUpdated(date);
        itemDescMapper.insert(itemDesc);

    }




}
